﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using SaleLibrary.Score;
using BrokerBase.Lib.Core;
using SaleLibrary.Tools;
using Broker.View.Comm;
using BrokerBase.Lib.Tool;

namespace Broker.Mgr.SaleView
{
    /// <summary>
    /// Interaction logic for ScoreRuleForm.xaml
    /// </summary>
    public partial class ScoreRuleForm
    {
        private IScoreRule scoreRule;
        private IScoreRule original;

        public ScoreRuleForm(IScoreRule sr)
        {
            original = sr;
            scoreRule = new ScoreRule();


            BaseTools.CopyTo<IScoreRule>(original, this.scoreRule);
            InitializeComponent();
            Loaded += new RoutedEventHandler(ScoreRuleForm_Loaded);
            PreviewClosed += new EventHandler<WindowPreviewClosedEventArgs>(ScoreRuleForm_PreviewClosed);
        }

        void ScoreRuleForm_PreviewClosed(object sender, WindowPreviewClosedEventArgs e)
        {
            if (IsCommit)
            {
                if (Verify())
                    BaseTools.CopyTo<IScoreRule>(scoreRule, original);
                else
                    e.Cancel = true;
            }
        }

        public bool IsCommit { get; set; }

        void ScoreRuleForm_Loaded(object sender, RoutedEventArgs e)
        {
            cmbEdition.ItemsSource = EditionUtil.GetEditions();
            DataContext = scoreRule;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = true;
            Close();

        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = false;
            Close();
        }

        private bool Verify()
        {

            if (cmbEdition.SelectedItem==null)
            {
                MessageBox.Show("版本号不能为空!");
                return false;
            }

            if (scoreRule.Score < 0)
            {
                MessageBox.Show("积分必须大于0");
                return false;
            }

            if (scoreRule.MaxLevel < 1 || scoreRule.MaxLevel > 16)
            {
                MessageBox.Show("最大层级必须大于1，小于16");
                return false;
            }

            if (!(scoreRule.Scale >= 0 && scoreRule.Scale <= 0.5))
            {
                MessageBox.Show("比例必须大于等于0，小于等于0.5");
                return false;
            }

            return true;
        }
    }
}
